﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using YXShop.BLL.Admin;
using System.Collections.Generic;
namespace YXShop.Web.admin.systeminfo
{
    public partial class bank_edit : System.Web.UI.Page
    {
        YXShop.BLL.SystemInfo.BankAccount bankBll = new YXShop.BLL.SystemInfo.BankAccount();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                YXShop.Common.PromptInfo.Popedom("010003002","对不起，您没有权限进行编辑");
                YXShop.Common.PromptInfo.Popedom("010003004", "对不起，您没有权限进行编辑");
                InitWebControl();
                BankBind();
                if (Request["Bank_ID"]!=null)
                {
                    string bankID = Request["Bank_ID"].ToString();
                    ViewState["Bank_ID"] = bankID;
                    YXShop.Model.SystemInfo.BankAccount model = bankBll.GetModelById(Convert.ToInt32(bankID));
                    if(model!=null){
                        this.txtName.Text = model.Bank_name;
                        this.ddlBankType.SelectedItem.Text = model.Bank_bankac;
                        this.txtNumber.Text = model.Bank_number;
                        this.txtOrdernum.Text = model.Bank_ordernum.ToString();
                        this.txtDiagram.Text = model.Bank_diagram;
                        this.rdPutout.SelectedValue = model.Bank_putout.ToString();
                        this.txtAccount.Text = model.Bank_account;
                        ViewState["puboutId"] = model.Bank_putoutid.ToString();
                        ViewState["icon"] = model.Bank_icon.ToString();
                        ViewState["puboutType"] = model.Bank_putouttypeid.ToString();
                        ViewState["putoutname"] = model.Bank_putoutname.ToString();
                    }
                }
            }
        }

        //保存事件
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            Save();
        }


        #region 验证
        /// <summary>
        /// 验证
        /// </summary>
        private void InitWebControl()
        {
            ChangeHope.WebPage.WebControl.Validate(this.txtName, "请填写帐户名称", "isnull", "必填", "该项为必填项");
            ChangeHope.WebPage.WebControl.Validate(this.txtOrdernum,"请填写排序号，数字越大表示权威越重，权重大的排前面","isint","必填","该项为必填项");
            this.Form.Attributes.Add("onsubmit", "return CheckForm()");
        }
        #endregion

        protected void BankBind()
        {
            YXShop.BLL.SystemInfo.BankInfo bll = new YXShop.BLL.SystemInfo.BankInfo();
            List<YXShop.Model.SystemInfo.BankInfo> model = bll.GetAll();
            ddlBankType.DataSource = model;
            ddlBankType.DataTextField = "BankName";
            ddlBankType.DataValueField = "ID";
            ddlBankType.DataBind();
        }

        #region 保存/修改
        //添加跟修改
        public void Save()
        {
            YXShop.Model.Admin.AdminInfo adminInfo = (YXShop.Model.Admin.AdminInfo)YXShop.Common.AdministrorManager.Get();           
            YXShop.Model.SystemInfo.BankAccount model = new YXShop.Model.SystemInfo.BankAccount();
            model.Bank_name = this.txtName.Text.Trim().ToString();
            model.Bank_bankac = this.ddlBankType.SelectedItem.Text;
            model.Bank_account = this.txtAccount.Text.Trim().ToString();
            if (txtNumber.Text.Trim()!="")
            {
                if (!ChangeHope.Common.ValidateHelper.IsNumber(txtNumber.Text.Trim()))
                {
                    this.ltlMsg.Text = "操作失败，卡号必须是数字组成" ;
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            model.Bank_number = this.txtNumber.Text.Trim().ToString();
            model.Bank_diagram = this.txtDiagram.Text.Trim().ToString();
            model.Bank_putout = Convert.ToInt32(this.rdPutout.SelectedValue);
            model.Bank_ordernum = Convert.ToInt32(this.txtOrdernum.Text.Trim().ToString());
            model.Bank_putoutid = ViewState["puboutId"]!=null?Convert.ToInt32(ViewState["puboutId"]):Convert.ToInt32(adminInfo.AdminId);
            model.Bank_putoutname = ViewState["putoutname"] != null ? ViewState["putoutname"].ToString() : adminInfo.AdminName;
            model.Bank_putouttypeid = ViewState["puboutType"] != null ? Convert.ToInt32(ViewState["puboutType"]) : 0;
            //图片参数
            YXShop.Common.SysParameter sys = new YXShop.Common.SysParameter();
            int ImagesThumbnailsHeight = sys.ImagesThumbnailsHeight;
            int ImagesThumbnailsWidth = sys.ImagesThumbnailsWidth;
            string fileStr = "";
            string path = "/yxuploadfile/payterrace/bank";
            ChangeHope.Common.UploadProcesedImages wm = new ChangeHope.Common.UploadProcesedImages();
            string ThumbnailsImage = string.Empty;
            string OriginalThumbnails = string.Empty;
            if (FileUploadBank.HasFile)
            {
                //原图
                ChangeHope.Common.UploadFile uf = new ChangeHope.Common.UploadFile();
                uf.ExtensionLim = ".gif,.jpg,.jpeg,.dmp";
                uf.FileLengthLim = 4000;
                uf.PostedFile = this.FileUploadBank;
                uf.SavePath = path;
                if (uf.Upload())
                {
                    if (uf.HaveLoad)
                    {
                        fileStr = uf.FilePath;
                        // 原图缩略图
                        wm.SourceImagePath = fileStr;
                        wm.ThumbnailImagePath = path;
                        wm.ThumbnailImageWidth = ImagesThumbnailsWidth;
                        wm.ThumbnailImageHeight = ImagesThumbnailsHeight;
                        if (wm.ToThumbnailImage())
                        {
                            OriginalThumbnails = wm.FilePath;
                        }
                        else
                        {
                            this.ltlMsg.Text = "操作失败，" + wm.Message + "";
                            this.pnlMsg.Visible = true;
                            this.pnlMsg.CssClass = "actionErr";
                            return;
                        }

                    }
                    wm.SaveWaterMarkImagePath = path;

                }
                else
                {
                    this.ltlMsg.Text = "操作失败，" + uf.Message + "";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
                model.Bank_icon = OriginalThumbnails;
            }
            else
            {
                if (ViewState["icon"] != null)
                {
                    model.Bank_icon = ViewState["icon"].ToString();
                }
                else
                {
                    this.ltlMsg.Text = "操作失败，请选择您要上传的图片。";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionErr";
                    return;
                }
            }
            if (ViewState["Bank_ID"] != null)
            {
                model.Bank_id = Convert.ToInt32(ViewState["Bank_ID"]);
                bankBll.Update(model);
                this.ltlMsg.Text = "操作成功，已修改该信息";
                this.pnlMsg.Visible = true;
                this.pnlMsg.CssClass = "actionOk";
            }
            else
            {
                int count = bankBll.Add(model);
                if (count > 0)
                {
                    this.ltlMsg.Text = "操作成功，已保存该信息";
                    this.pnlMsg.Visible = true;
                    this.pnlMsg.CssClass = "actionOk";
                }
                else
                {
                    this.ltlMsg.Text = "操作失败";
                    this.pnlMsg.Visible = false;
                    this.pnlMsg.CssClass = "actionErr";
                }
            }
        }
        #endregion
     
    }
}
